/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { useRef, useEffect } from 'react';
import { PageContainer } from '@ant-design/pro-layout';
import DragTable from '@/components/DragTable';
import ProTable from '@ant-design/pro-table';
import type { ActionType } from '@ant-design/pro-table';
import styles from './index.less';
import { getApplyRecord, saveApplyCash } from '@/services/finance_amountverify';
import { Button, Space, Popconfirm } from 'antd';
import type { StringObjType } from '@/components/_pages/AmountApply/index.d';
import { applyLogColumn } from '@/components/_pages/AmountApply/index.d';
import { usePageTabsChange } from '@/utils/hooks/pageTabs';

const AmountCashVerify: React.FC = () => {
  const [show] = usePageTabsChange();
  const actionRef = useRef<ActionType>();

  useEffect(() => {
    if (show) {
      actionRef.current?.reload();
    }
  }, [show]);

  const confirmAllow = async (record: StringObjType) => {
    const { id } = record;
    const { code } = await saveApplyCash({ id });
    if (code == 0) {
      actionRef.current?.reload();
    }
  };

  return (
    <PageContainer className={styles.main} title=''>
      <DragTable>
        <ProTable<StringObjType>
          className='kb-pro-table'
          actionRef={actionRef}
          rowKey='id'
          request={getApplyRecord}
          pagination={{
            pageSize: 20,
            showQuickJumper: false,
            showSizeChanger: false,
          }}
          columns={[
            ...applyLogColumn,
            {
              title: '操作',
              dataIndex: 'operate',
              align: 'center',
              search: false,
              render: (_, record) => {
                const { deduction_status } = record;
                return (
                  <>
                    {`${deduction_status}` === '2' ? (
                      <Space>
                        <Popconfirm
                          title={`确认重新扣款吗？`}
                          onConfirm={confirmAllow.bind(null, record)}
                        >
                          <Button size='small' type='primary'>
                            重新扣款
                          </Button>
                        </Popconfirm>
                      </Space>
                    ) : null}
                  </>
                );
              },
            },
          ]}
          options={false}
          scroll={{ x: 1200 }}
          search={false}
        />
      </DragTable>
    </PageContainer>
  );
};

export default AmountCashVerify;
